<template>
	<view>
		<!-- 状态栏 -->
		<page-status></page-status>
		<!-- 顶部导航栏 -->
		<page-head></page-head>
		<view class="category-list">
			<!-- 左侧分类导航 -->
			<scroll-view class="left" scroll-y="true" >
				<view 
					class="row"
					v-for="(category,index) in categoryList"
					:key="category.id"
					:class="{on: index == showCategoryIndex}"
					@tap="showCategory(index)"
				>
					<view class="text">
						<view class="block"></view>
						{{category.title}}
					</view>
				</view>
			</scroll-view>
			<!-- 右侧子导航 -->
			<scroll-view class="right" scroll-y="true">
				<view 
					class="category"
					v-for="(category, index) in categoryList"
					:key="category.id"
					v-show="index == showCategoryIndex"
				>
					<view class="banner">
						<image :src="category.banner"></image>
					</view>
					<view class="list">
						<view 
							class="box"
							v-for="(box, i) in category.list"
							:key="i"
							@tap="toCategory(box)"
						>
							<image :src="'/static/img/category/list/' + box.img"></image>
							<view class="text">{{box.name}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import pageHead from '../home/pageHeader.vue'
	import interfaces from '../../../utils/interfaces.js'

	export default {
		data() {
			return {
				categoryList: [],
				showCategoryIndex: 0
			}
		},
		components: {
			pageHead
		},
		onLoad() {
			this.initData()
		},
		methods: {
			initData(){
				this.request({
					url:interfaces.getCategory,
					success:((res)=>{
						this.categoryList = res.data
					})
				})
			},
			showCategory(index) {
				this.showCategoryIndex = index
			},
			toCategory(e) {
				uni.navigateTo({
					url: '../../goods/goodsList?name=' + e.name
				})
			}
		}
	}
</script>

<style lang="scss">
	.category-list {
		width: 100%;
		display: flex;
		background-color: #fff;
		
		.left {
			width: 24%;
			background-color: #f2f2f2;
			
			.row {
				width: 100%;
				height: 90rpx;
				display: flex;
				align-items: center;
				
				.text {
					width: 100%;
					display: flex;
					justify-content: center;
					position: relative;
					font-size: 28rpx;
					color: #3c3c3c;
					.block {
						width: 0rpx;
						position: absolute;
						left: 0;
					}
				}
			
				&.on {
					height: 100rpx;
					background-color: #fff;
					
					.text {
						font-size: 30rpx;
						font-weight: 600;
						color: #2d2d2d;
						block {
							width: 10rpx;
							height: 80%;
							background-color: #f06c7a;
						}
					}
				}
			}
		
		}
	
		.right {
			width: 76%;
			position: absolute;
			left: 24%;
			
			.category {
				width: 94%;
				padding: 20rpx 3%;
				
				.banner {
					width: 100%;
					height: 28vw;
					border-radius: 10rpx;
					box-shadow: 0rpx 5rpx 20rpx rgba(0, 0, 0, 0.3);
					
					image {
						width: 100%;
						height: 100%;
					}
				}
			
				.list {
					width: 100%;
					margin-top: 40rpx;
					display: flex;
					flex-wrap: wrap;
					
					.box {
						width: calc(71.44vw / 3);	/* 76*0.94 = 71.44 */
						margin-bottom: 30rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						flex-wrap: wrap;
						
						image {
							width: 60%;	/* 宽度是相对与父元素.box的宽度 71.44/3 的60% */
							height: calc(71.44vw / 3 * 0.6);	/* 高度与宽度相同，width ==height */
						}
						
						.text {
							margin-top: 5rpx;
							width: 100%;
							display: flex;
							justify-content: center;
							font-size: 26rpx;
						}
					}
				}
			}
		}
	}
</style>
